package com.power.sifa.approve.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.approve.api.IAprv_materials_customerFeign;
import com.power.sifa.approve.model.Aprv_materials_customer;
import com.power.sifa.approve.model.pageInfo.Aprv_materials_customerPageInfo;
import com.power.sifa.approve.service.Aprv_materials_customerService;
import com.power.sifa.approve.service.Aprv_process_materialsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   自定义过程材料控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Aprv_materials_customerController",tags={"自定义过程材料"})
public class Aprv_materials_customerController implements IAprv_materials_customerFeign {

    @Resource
    private Aprv_materials_customerService aprv_materials_customerService;

    @Autowired
    private Aprv_process_materialsService aprv_process_materialsService;

    
    @Override
    @ApiOperation("查询自定义过程材料分页列表")
    @SaCheckPermission("aprv_materials_customer_list")
    public ResponseDTO list(Aprv_materials_customerPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Aprv_materials_customer> page = this.aprv_materials_customerService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Aprv_materials_customer aprv_materials_customer : page) {
                aprv_materials_customer.geneText();
                list.add(aprv_materials_customer);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看自定义过程材料")
    @SaCheckPermission("aprv_materials_customer_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Aprv_materials_customer aprv_materials_customer  = aprv_materials_customerService.getById(id);
        if (aprv_materials_customer != null) {
            aprv_materials_customer.geneText();
        }
        return ResponseDTO.succData(aprv_materials_customer);
    }

    @Override
    @ApiOperation("删除自定义过程材料")
    @SaCheckPermission("aprv_materials_customer_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        aprv_materials_customerService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改自定义过程材料")
    @SaCheckPermission("aprv_materials_customer_save")
    public ResponseDTO save(Aprv_materials_customer aprv_materials_customer) {
        Assert.notNull(aprv_materials_customer, "aprv_materials_customer不能为空");
        aprv_materials_customerService.saveOrUpdate(aprv_materials_customer);
        return ResponseDTO.succData(aprv_materials_customer);
    }
    
    @Override
    @ApiOperation(value="批量删除自定义过程材料")
    @SaCheckPermission("aprv_materials_customer_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        aprv_materials_customerService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("aprv_materials_customer_list")
    public void output(HttpServletResponse response, Aprv_materials_customerPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "自定义过程材料";
        String[] heads = {"nanoid","引用主表id","顺序","材料类型名称","材料描述","过程类型"};
        String[] fields = {"id","applyid","sortnum","name","describe","proctype"};
        Page<Aprv_materials_customer> page = this.aprv_materials_customerService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "自定义过程材料";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"nanoid","引用主表id","顺序","材料类型名称","材料描述","过程类型"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","applyid","sortnum","name","describe","proctype"};
        List<Aprv_materials_customer> list = (List<Aprv_materials_customer>)ExcelUtil.Import(fields, file, Aprv_materials_customer.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.aprv_materials_customerService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    /**
     * 通过applyid查询自定义材料类型
     * @param applyid
     * @return
     */
    @Override
    public ResponseDTO queryCustomerMaterialByApplyid(String applyid) {
        Assert.notNull(applyid, "applyid不能为空");
        return ResponseDTO.succData(this.aprv_materials_customerService.listCustomerByApplyid(applyid));
    }

    /**
     * 删除自定义过程材料
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO deleteCustomer(String id) {
        Assert.notNull(id, "id不能为空");
        try {
            this.aprv_process_materialsService.deleteByCustomerId(id);
            this.aprv_materials_customerService.removeById(id);
            return ResponseDTO.succ();
        } catch (Exception e) {
            log.error("删除自定义材料类型失败");
            e.printStackTrace();
            return ResponseDTO.wrap(500,"删除自定义材料类型失败");
        }
    }

}
